import { fetchTagList } from '../../api'

interface TagItemState {
	'title': string,
	'url': string,
	'id': number,
	'focus': number,
	'message': number
}

interface State {
	tag: TagItemState[],
	focusTag: TagItemState[]
}

const initState: State = {
	tag: [],
	focusTag: []
}

export function fetstTagAllList() {
	return async (dispatch) => {
		const resp = await fetchTagList();
		const { data } = resp.data;
		dispatch({ type: SET_TAG, payload: data })
	}
}

const SET_TAG = 'SET_TAG'

interface SetTagType {
	type: typeof SET_TAG,
	payload: TagItemState[]
}

type ActionType = SetTagType

const tagReducer = (state = initState, action: ActionType) => {
	switch (action.type) {
		case SET_TAG:
			return {
				...state,
				tag: action.payload
			}
		default:
			return state;
	}
}

export default tagReducer